var rootPath = getRootPath();

$(function() {
	$.ajax({
		url: rootPath + '/tag/selectPageTag',
        type: 'POST',
        contentType : 'application/json;charset=UTF-8',
        data: JSON.stringify({
        	"dr" : 1
        }),
        success: function (data) {
        	fillTag(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	layer.alert("error:(");
        }
    });
	
	$("#tagName").keyup(function(event) {
		if(event.keyCode == 13) {
			$("#searchTag").click();
		}
	});
	$("#searchTag").click(function() {
		var tagName = $("#tagName").val();
		$.ajax({
			url: rootPath + '/tag/selectPageTag',
	        type: 'POST',
	        contentType : 'application/json;charset=UTF-8',
	        data: JSON.stringify({
	        	"dr" : 1,
	        	"tagName" : tagName
	        }),
	        success: function (data) {
	        	$("#tags").empty();
	        	fillTag(data);
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	        	layer.alert("error:(");
	        }
	    });
	});
});

/*标签-添加-编辑*/
function tag_add_edit(title,url,w,h){
	layer_show(title,url,w,h);
}

function tag_del(obj,id){
	var dataNum = parseInt($("#dataNum").html());
	layer.confirm('标签删除须谨慎，确认要删除吗？',function(index){
		if(id != "") {
			$.ajax({
				url: rootPath + '/tag/' + id,
				type: 'DELETE',
				success: function(data){
					if(data.returnCode == "0000") {						
						$(obj).parents("tr").remove();
						layer.msg('已删除!',{icon:1,time:1000});
						
						$("#dataNum").html(dataNum - 1);
					} else {
						layer.alert(data.returnMessage);
					}
				},
				error:function(data) {
					layer.alert("error:(");
				},
			});		
		}
	});
}

function datadel() {
	var tagIds = new Array();
	$("input:checkbox[name='tagCheckbox']:checked").each(function(k) {
		tagIds.push($(this).val());
	});
	var length = tagIds.length;
	var dataNum = parseInt($("#dataNum").html());
	if(length > 0) {
		layer.confirm('标签删除须谨慎，确认要删除吗？',function(index){
			$.ajax({
				url: rootPath + '/tag/deleteTags',
				type: 'POST',
				traditional: true,
				data: JSON.stringify(tagIds),
	            contentType: 'application/json;charset=UTF-8',
	            dataType:"json",
				success: function(data){
					if(data.returnCode == "0000") {						
						$("input:checkbox:checked").each(function(k) {
							$(this).parents("tr[name='tagTr']").remove();
						});
						layer.msg('已删除!',{icon:1,time:1000});
						$("#dataNum").html(dataNum - length);
					} else {
						layer.alert(data.returnMessage);
					}
				},
				error:function(data) {
					layer.alert("error:(");
				},
			});	
		});
	} else {
		layer.alert("请选择需要删除的标签!!!");
	}
}

function switchPage(pageNo) {
	$(this).css("cursor", "pointer");
	var tagName = $("#tagName").val();
	$.ajax({
		url : rootPath + '/tag/selectPageTag',
		type : 'POST',
		contentType : 'application/json;charset=UTF-8',
		data : JSON.stringify({
			"dr" : 1,
			"tagName" : tagName,
			"pageNo" : pageNo,
		}),
		success : function(data) {
			fillTag(data);
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error:(");
		}
	});
}

function fillTag(data) {
	if(data.returnCode == "0000") {
		var length = data.returnObject.list.length;
		$("#dataNum").html(data.returnObject.total);
		var tr;
		for(var i = 0;i < length;i++) {
			tr += "<tr class='text-c' name='tagTr'>";
			tr += "<td><input type='checkbox' value='" + data.returnObject.list[i].tagId + "' name='tagCheckbox'></td>";
			tr += "<td>" + data.returnObject.list[i].tagName + "</td>";
			tr += "<td>" + data.returnObject.list[i].tagWeight + "</td>";
			tr += "<td>" + (data.returnObject.list[i].tagDesc == undefined ? "" : data.returnObject.list[i].tagDesc) + "</td><td>";
			tr += "<a title='编辑' href='javascript:;' onclick='tag_add_edit(\"编辑标签\"," + "\"tagEditPage?tagId=" + data.returnObject.list[i].tagId + "\", \"\",\"\")' style='text-decoration:none'><i class='iconfont marginLeft12'>&#xe60a;</i></a>";
			tr += "<a title='删除' href='javascript:;' onclick='tag_del(this," + "\"" + data.returnObject.list[i].tagId + "\")' class='ml-5' style='text-decoration:none'><i class='iconfont marginLeft12'>&#xe612;</i></a></td></tr>";
		}
		$("#tags").empty();
		$("#tags").append(tr);
		
		var page = "";
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ 1 + ")'>首页</span>";
		if (data.returnObject.hasPreviousPage) {
			page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.prePage + ")'>上一页</span>";
		}
		var num = data.returnObject.navigatepageNums.length;
		for (var i = 0; i < num; i++) {
			if (data.returnObject.navigatepageNums[i] == data.returnObject.pageNum) {
				page += "<span style='font-weight:900;padding:7px 10px;'>"
						+ data.returnObject.pageNum + "</span>";
			} else {
				page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
						+ data.returnObject.navigatepageNums[i]
						+ ")'>"
						+ data.returnObject.navigatepageNums[i] + "</span>";
			}
		}
		if (data.returnObject.hasNextPage) {
			page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.nextPage + ")'>下一页</span>";
		}
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ data.returnObject.pages + ")'>末页</span>";

		$("#page").empty();
		$("#page").append(page);
		$(".pageNavigate").each(function() {
			$(this).hover(function() {
				$(this).css("cursor","pointer");
			});
		});
	} else {
		$("#dataNum").html("0");
	}
}